#ifndef ITEMREFERENCE_H
#define ITEMREFERENCE_H


#include "models_global.h"
#include "ValueItem.h"


#include <QSharedData>
#include <QSharedDataPointer>
#include <QPointer>


class MODELSSHARED_EXPORT ReferenceItem : public ValueItem
{
    Q_OBJECT

    class Data : public QSharedData
    {
    public:
        Data() {}
        Data(const Data& other) : QSharedData(other)
        {
            refItem = other.refItem;
            refName = other.refName;
        }

        QPointer<ValueItem> refItem;
        QString             refName;
    };

public:

    enum DataType
    {
        DataType_FIRST = ValueItem::DataType_LAST + 1,

        ReferenceData = DataType_FIRST,

        DataType_LAST = ReferenceData
    };

    Q_INVOKABLE ReferenceItem(ValueItem *parent = 0, ValueItem* referenced = 0);
    ReferenceItem(const ReferenceItem& other) : ValueItem(other)
    {
        d = other.d;
    }


    bool isValid() const;

    virtual int fieldCount() const;
    virtual QString fieldName(const int &field) const;
    virtual int field(const QString &field_name) const;

    QString referencedName() const;
    ValueItem* referencedItem() const;


public slots:

    void onRefreshReferenceItem();
    void onReferenceItemDirty();


protected:


    virtual QVariant customData(const int &field, int role = Qt::DisplayRole) const;
    virtual void setCustomData(const int &field, const QVariant &value);


private:

    QSharedDataPointer<Data> d;

    friend class ReferenceItem;

};



Q_DECLARE_METATYPE(ReferenceItem*);


#define IS_REFERENCE(item) (QMetaType::type("ItemReference") == QMetaType::type(item->metaObject()->className()))
#define IS_REFERENCE_TYPE(type) (QMetaType::type("ItemReference") == type)


#endif // ITEMREFERENCE_H
